package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;

public class SetWifiActivity extends Activity {
    private ArrayList<ScanResult> mWifiList;
    WifiAdmin wifiAdmin;

    String str2;
    EditText et1;
    MyApplication m;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_wifi);
        Spinner sp = (Spinner) findViewById(R.id.sp_ato2);
        m = (MyApplication) getApplication();
        wifiAdmin = new WifiAdmin(this);
        wifiAdmin.openWifi();
        wifiAdmin.startScan();
        mWifiList = (ArrayList<ScanResult>) wifiAdmin.getWifiList();
        ArrayList<String> s = new ArrayList<>();
        for (int i = 0; i < mWifiList.size(); i++) {
            if (mWifiList.get(i).SSID.length() < 9) {
                s.add(mWifiList.get(i).SSID);
            } else {
                String m_SSID = mWifiList.get(i).SSID.substring(0, 9);
                if (!"FeiXueKJ_".equals(m_SSID)) {
                    s.add(mWifiList.get(i).SSID);
                }
            }
        }
        Spinneradapter sp5 = new Spinneradapter(s, SetWifiActivity.this);
        et1 = (EditText) findViewById(R.id.et1_ato);
        sp.setAdapter(sp5);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                str2 = parent.getItemAtPosition(position).toString();
                m.Host_SSid = str2;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        Spinner sp2 = (Spinner) findViewById(R.id.sp_ato);
        ArrayList<String> s2 = new ArrayList<>();
        s2.add("WPA2PSK");
        s2.add("WPAPSK");
        s2.add("SHARED");
        s2.add("OPEN");
        Spinneradapter sp6 = new Spinneradapter(s2, SetWifiActivity.this);
        sp2.setAdapter(sp6);
        sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                String str = parent.getItemAtPosition(position).toString();

                if ("WPA2PSK".equals(str)) {
                    m.Host_jami = "03";
                } else if ("WPAPSK".equals(str)) {
                    m.Host_jami = "02";
                } else if ("SHARED".equals(str)) {
                    m.Host_jami = "01";
                } else {
                    m.Host_jami = "00";
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });
    }

    public void click(View v) {
        String password = et1.getText().toString();
        if (str2 == null) {
            HttpConnection.showToast(getBaseContext(),"无wifi网络不可配置网关");
        } else if (TextUtils.isEmpty(password)) {
            HttpConnection.showToast(getBaseContext(),"请配置Wifi密码");
        } else {
            m.c.close();
            m.flag8=1;
            m.flag6=true;
            m.Host_Password = password;
            Intent it = new Intent(SetWifiActivity.this, WifiConnection.class);
            startActivity(it);
            finish();
        }

    }




    public static byte uniteByte(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }

    public static byte[] chatOrder(byte[] m) {
        if (m.length % 2 == 0) {
            byte[] bytes = new byte[m.length / 2];
            for (int i = 0, j = 0; i < m.length; i += 2, j++) {
                bytes[j] = uniteByte(m[i], m[i + 1]);
            }
            return bytes;
        }

        return null;
    }

    public static byte[] chatOrders(String c) {
        byte[] m = c.getBytes();
        return chatOrder(m);
    }

    /*
     Runnable sendThread = new Runnable() {

         @Override
         public void run() {

             byte[] sendBuffer = null;

             try {

             } catch (Exception e1) {

                 e1.printStackTrace();
             }

             try {
                 outStream = socket.getOutputStream();
             } catch (IOException e) {

                 e.printStackTrace();
             }
             try {
                 Log.i("test5", "strMessage=" + strMessage);
                 outStream.write(hex2Byte(strMessage));
             } catch (IOException e) {

                 e.printStackTrace();
             }
         }
     };*/
    private Spinner sp;


    /**
     * 16进制字符串转换成byte数组
     *
     * @return 转换后的byte数组
     * @param16进制字符串
     */
    public static byte[] hex2Byte(String hex) {
        String digital = "0123456789ABCDEF";
        char[] hex2char = hex.toCharArray();
        byte[] bytes = new byte[hex.length() / 2];
        int temp;
        for (int i = 0; i < bytes.length; i++) {
            // 其实和上面的函数是一样的 multiple 16 就是右移4位 这样就成了高4位了
            // 然后和低四位相加， 相当于 位操作"|"
            // 相加后的数字 进行 位 "&" 操作 防止负数的自动扩展. {0xff byte最大表示数}
            temp = digital.indexOf(hex2char[2 * i]) * 16;
            temp += digital.indexOf(hex2char[2 * i + 1]);
            bytes[i] = (byte) (temp & 0xff);
        }
        return bytes;
    }

    public static class handle {
        public int state;
        public String info;
        public String result;
    }

    /**
     * 字符串转换成十六进制字符串
     *
     * @return String 每个Byte之间空格分隔，如: [61 6C 6B]
     * @paramString str 待转换的ASCII字符串
     */
    public static String str2HexStr(String str) {

        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            //sb.append(' ');
        }
        return sb.toString().trim();
    }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

        }
        return d;
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static byte GetXOR(byte[] Cmd) {
        if (Cmd.length > 1) {
            byte check = (byte) (Cmd[0] ^ Cmd[1]);
            for (int i = 2; i < Cmd.length; i++) {
                check = (byte) (check ^ Cmd[i]);
            }
            return check;
        } else {
            return Cmd[0];
        }
    }


}
